<?php

class Application_Model_Element {

    protected $_id;

    public function id() {
        return $this->_id;
    }

    public function set_id($id) {
        $this->_id = $id;
    }

    protected $_name;

    public function name() {
        return $this->_name;
    }

    public function set_name($name) {
        $this->_name = $name;
    }

    protected $_tech_id;

    public function tech_id() {
        return $this->_tech_id;
    }

    public function set_tech_id($tech_id) {
        $this->_tech_id = $tech_id;
    }

    protected $_total_size;

    public function total_size() {
        return $this->_total_size;
    }

    public function set_total_size($total_size) {
        $this->_total_size = $total_size;
    }

    protected $_available_size;

    public function available_size() {
        return $this->_available_size;
    }

    public function set_available_size($available_size) {
        $this->_available_size = $available_size;
    }

    protected $_owner_id;

    public function owner_id() {
        return $this->_owner_id;
    }

    public function set_owner_id($owner_id) {
        $this->_owner_id = $owner_id;
    }

    protected $_owner;

    public function owner() {
        return $this->_owner;
    }

    public function set_owner($owner) {
        $this->_owner = $owner;
    }

    protected $_status;

    public function status() {
        return $this->_status;
    }

    public function set_status($status) {
        $this->_status = $status;
    }

    protected $_tracker;

    public function tracker() {
        return $this->_tracker;
    }

    public function set_tracker($tracker) {
        $this->_tracker = $tracker;
    }

    protected $_type;

    public function type() {
        return $this->_type;
    }

    public function set_type($type) {
        $this->_type = $type;
    }


    protected $_image_uri;
    protected $_groups;
    protected $_meta;

    public function description() {
        return $this->_meta->description();
    }

    public function link() {
        return $this->_meta->link();
    }

    public function groups() {
        return $this->_groups;
    }

    function __construct($id, $name, $tech_id, $total_size, $available_size, $owner_id, $status, $tracker, $type) {
        $this->_id = $id;
        $this->_name = $name;
        $this->_tech_id = $tech_id;
        $this->_total_size = $total_size;
        $this->_available_size = $available_size;
        $this->_owner_id = $owner_id;
        $this->_status = $status;
        $this->_tracker = $tracker;
        $this->_type = $type;
        if ($this->_id) {
            $this->getMetadata();
        }
    }

    public function fillGroups() {
        $gmapper = new Application_Model_GroupMapper();
        $this->_groups = $gmapper->findLinkedGroups($this);
    }

    protected function getMetadata() {
        $mdmapper = new Application_Model_MetadataMapper();
        $this->_meta = $mdmapper->findForElement($this->_id);
    }

    public function get_image_uri() {
        if (!$this->_image_uri) {
            $imgmapper = new Application_Model_ImageMapper();
            if (!$this->_meta || $this->_meta->image_id() == null) {
                if (!$this->_groups) {
                    $this->fillGroups();
                }
                foreach ($this->_groups as $group) {
                    if (!$group->categorizing() && $group->image_id()) {
                        $this->_image_uri = $imgmapper->find($group->image_id())->uri();
                        return $this->_image_uri;
                    } elseif ($group->image_id()) {
                        $this->_image_uri = $imgmapper->find($group->image_id())->uri();
                    }
                }
                if (!$this->_image_uri) {
                    $this->_image_uri = '/images/dflt_group.jpg';
                }
            } else {
                $this->_image_uri = $imgmapper->find($this->_meta->image_id())->uri();
            }
        }
        return $this->_image_uri;
    }

}
